package com.hhkj.admin.invoice.service.base.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.hhkj.admin.invoice.common.cont.InvoiceConst;
import com.hhkj.admin.invoice.domain.InvoiceTitle;
import com.hhkj.admin.invoice.mapper.InvoiceTitleMapper;
import com.hhkj.admin.invoice.model.dopo.InvoiceTitleDo;
import com.hhkj.admin.invoice.model.dopo.InvoiceTitlePo;
import com.hhkj.admin.invoice.service.base.InvoiceTitleService;
import com.ruoyi.common.base.biz.service.base.BaseServiceImpl;
import com.ruoyi.common.cont.SysConst;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author admin
 */
@Service
public class InvoiceTitleServiceImpl
        extends BaseServiceImpl<InvoiceTitle, InvoiceTitleDo, InvoiceTitlePo, InvoiceTitleMapper>
        implements InvoiceTitleService {

    @Transactional
    @Override
    public boolean save(InvoiceTitle entity) {
        if (SysConst.Flag.IS.getFlag().equals(entity.getDefaultFlag())) {
            LambdaUpdateWrapper<InvoiceTitle> updateWrapper
                    = new LambdaUpdateWrapper<>();
            updateWrapper.eq(InvoiceTitle::getSaleId, entity.getSaleId())
                    .set(InvoiceTitle::getDefaultFlag, SysConst.Flag.NO.getFlag());
            super.update(updateWrapper);
        }
        return super.save(entity);
    }

    @Override
    public boolean clearDefaultFlag(Long saleId) {
        LambdaUpdateWrapper<InvoiceTitle> updateWrapper = new LambdaUpdateWrapper<InvoiceTitle>()
                .eq(InvoiceTitle::getSaleId, saleId)
                .eq(InvoiceTitle::getDefaultFlag, SysConst.Flag.IS.getFlag())
                .set(InvoiceTitle::getDefaultFlag, SysConst.Flag.NO.getFlag());
        return update(updateWrapper);
    }
}




